<?php

global $footer, $data;

class News extends CI_Controller {

    public function __construct() {
        parent::__construct();
        global $footer, $data;
        if ($this->session->userdata('lang') == 'spanish') {
            $this->lang->load('about', 'spanish');
            $footer = "footer";
            $data['icon_lang'] = "en.jpg";
        } else {
            $this->lang->load('about', 'english');
            $footer = "footer_en";
            $data['icon_lang'] = "es.jpg";
        }
        $this->cargarIdioma();
        $data['place'] = "news";
        $this->registrar_visita($data['place']);
    }

    public function index() {
        redirect(base_url());
    }

    public function articulo($id) {
        global $footer, $data;

        if ($this->session->userdata('lang') == 'spanish') {
            $data['noticia'] = $this->ciempies_model->NoticiaDetailEs($id);
            $footer = "footer";
        } else {
            $data['noticia'] = $this->ciempies_model->NoticiaDetailEn($id);
            $footer = "footer_en";
        }

        $data['imagen'] = $this->ciempies_model->imagenesNoticia($id);
        $data['nota'] = $this->noticiasRelacionadas($id);

        $this->load->view('template/front_end/header', $data);
        $this->load->view('front_end/noticia_main');
        $this->load->view('front_end/noticia_news');
        $this->load->view('template/front_end/' . $footer);
    }

    private function noticiasRelacionadas($id) {
        $r = $this->ciempies_model->keywords($id);
        $palabra = explode(' ', $r->KEYW);
        $listaNoticiasRel = array();
        foreach ($palabra as $p) {
            array_push($listaNoticiasRel, $this->ciempies_model->relacionados($p, $id));
        }
        $listaFiltrada = array();
        $lista = array_filter($listaNoticiasRel);
        foreach ($lista as $a) {
            foreach ($a as $b) {
                if (!in_array($b->IDNOTICIA, $listaFiltrada)) {
                    array_push($listaFiltrada, $b->IDNOTICIA);
                }
            }
        }

        $nota = array();
        if ($this->session->userdata('lang') == 'spanish') {
            foreach ($listaFiltrada as $id) {
                array_push($nota, $this->ciempies_model->notaRel_Es($id));
            }
        } else {
            foreach ($listaFiltrada as $id) {
                array_push($nota, $this->ciempies_model->notaRel_En($id));
            }
        }

        return $nota;
    }

    function cargarIdioma() {
        global $footer, $data;
        /*  traduccion del menú */
        $data['headerTituloNotros'] = $this->lang->line('headerTituloNotros');
        $data['headerTituloDescarg'] = $this->lang->line('headerTituloDescarg');
        $data['headerTituloHerram'] = $this->lang->line('headerTituloHerram');
        $data['headerTituloDocu'] = $this->lang->line('headerTituloDocu');
        $data['headerTituloCol'] = $this->lang->line('headerTituloCol');
        $data['headerTituloNoticias'] = $this->lang->line('headerTituloNoticias');
        $data['headerTituloContcato'] = $this->lang->line('headerTituloContcato');
        $data['newsNewsTitle'] = $this->lang->line('newsNewsTitle');
    }

    public function lenChg() {
        global $data;
        if ($this->session->userdata('lang') == 'spanish') {
            $this->session->set_userdata('lang', 'english');
        } else {
            $this->session->set_userdata('lang', 'spanish');
        }
        redirect(base_url() . $data['place']);
    }

    public function registrar_visita($place) {
        $v_sitio = "ciempiess.org/$place";
        $v_date = date('Y-m-j H:i:s');
        $v_ip = $this->input->ip_address();
        $this->ciempies_model->registrar_visita($v_sitio, $v_date, $v_ip);
    }

}
